package com.itheima;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

/**
 * @auther Macrydy
 * @date 2020/7/1 9:33
 */
public class MapDemo {
    public static void main(String[] args) {
        HashMap<Integer,String> map = new HashMap<>();
        map.put(1, "张三");
        map.put(2, "李四");
        map.put(3, "王五");

        System.out.println(map);

        System.out.println(map.isEmpty());

        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, String> entry = iterator.next();
//            iterator.remove();
            System.out.println(entry.getKey()+","+entry.getValue());
        }

        //遍历map集合的第一种方式
        Set<Integer> keySet = map.keySet();
        for (Integer key : keySet) {
            System.out.println("key:"+key+"......"+"value:"+map.get(key));

        }
        //遍历map集合的第二种方式
        Set<Map.Entry<Integer, String>> entries = map.entrySet();
        for (Map.Entry<Integer, String> entry : entries) {
            System.out.println("key:"+entry.getKey()+"......"+"value:"+entry.getValue());
        }
        //遍历map集合的第三种方式
        map.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.println(integer+","+s);
            }
        });

    }
}
